from typing import Annotated, Any, Literal, Type, Tuple
from pydantic_settings import BaseSettings, SettingsConfigDict, JsonConfigSettingsSource, PydanticBaseSettingsSource
from functools import lru_cache
from enum import Enum


class Environment(Enum):
    LOCAL = "local"
    STAGING = "staging"
    PRODUCTION = "production"

class Settings(BaseSettings):
    # 优先读取环境变量，没有环境变量读取env_file设置的配置文件
    model_config = SettingsConfigDict(
        json_file="local.env.json", env_ignore_empty=True, json_file_encoding='utf-8', env_file_encoding='utf-8'
    )

    ENV: Environment = Environment.LOCAL
    API_VERSION: str
    TOKEN_SIGN_KEY: str
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 # 60 minutes * 24 hours * 8 days = 8 days

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        return (
            init_settings,
            JsonConfigSettingsSource(settings_cls),
            env_settings,
            file_secret_settings,
        )

settings = Settings()

@lru_cache
def get_settings():
    return settings